import 'package:flutter/material.dart';
import 'package:my_app/common/common.dart';
import 'package:my_app/service/api/api.dart';
import 'package:my_app/single/single_goods.dart';

class InputGoodsCode extends StatefulWidget {
  InputGoodsCode({Key? key, this.title}) : super(key: key);
  final String? title;
  @override
  State<InputGoodsCode> createState() => _InputGoodsCode(title: this.title);
}

class _InputGoodsCode extends State<InputGoodsCode> {
  final String? title;
  _InputGoodsCode({this.title});
  double paddingLeft = 45.0;
  double itemWidth = 140.0;
  // button 文字样式
  TextStyle _buttonTextStyle = TextStyle(
    fontSize: 10.0,
    color: Colors.white,
  );
  // button 样式
  ButtonStyle _buttonStyle = TextButton.styleFrom(
    backgroundColor: Color(Common.muji),
    // padding: EdgeInsets.all(1),
  );

  String goodsCode = ""; // 带入的商品码
  TextEditingController _controller = new TextEditingController();
  @override
  initState() {
    // alertDialog("123");
    super.initState();
    // Map userinfo = {"userName":"张三","age":20};
  }

  @override
  didChangeDependencies() {
    print('did change dependencies');

    super.didChangeDependencies();
  }

  // @override
  // didUpdateWidget(TestStatefulWidget oldWidget) {
  //   print('did update widget');
  //   super.didUpdateWidget(oldWidget);
  // }
  @override
  deactivate() {
    print('deactivate');
    super.deactivate();
  }

  @override
  dispose() {
    print('dispose');
    super.dispose();
  }

  @override
  reassemble() {
    print('reassemble');
    super.reassemble();
  }

// 修改商品条码
  void changeGoods(String goods) async {
    var data = await ApiService.changeGoodsCode(goods);
    if (data.length > 0) {
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) {
            return SingleGoods(goodsCode: goodsCode);
            // return TimePickerPage();
          },
        ),
      ).then((value) => _controller.text = "");
    } else {
      Common.showTost("商品不存在");
    }
    print(data);
  }

  @override
  Widget build(BuildContext context) {
    // final ButtonStyle buttonStyle = Common.buttonStyle;
    // TextStyle buttonTextStyle = Common.buttonTextStyle;

    return Scaffold(
        appBar: AppBar(
          title: Text('批量扫描'),
          centerTitle: true,
          backgroundColor: Color(Common.muji),
          leading: IconButton(
            tooltip: '返回',
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ),
        body: Container(
          padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
          child: Row(
            children: [
              Container(
                width: 200,
                height: 30,
                child: TextField(
                  onChanged: (value) {
                    goodsCode = value;
                  },
                  controller: _controller,
                  autofocus: true,
                  keyboardType: TextInputType.number,
                  onSubmitted: (value) {
                    if (goodsCode.length > 2) {
                      if (goodsCode == "" ||
                          goodsCode.substring(0, 2) == "QR") {
                        Common.showTost("商品条码格式错误");
                        return;
                      } else {
                        changeGoods(goodsCode);
                      }
                    } else {
                      Common.showTost("商品条码格式错误");
                    }
                  },
                  decoration: InputDecoration(
                    focusedBorder: Common.focusedBorder,
                    border: OutlineInputBorder(
                        // borderSide: BorderSide(123
                        //     color: Colors.black, width: 2.0),
                        ),
                  ),
                ),
              ),
              Container(
                width: 60,
                height: 30,
                child: TextButton(
                  style: _buttonStyle,
                  child: Text("前往赋码", style: _buttonTextStyle),
                  onPressed: () {
                    if (goodsCode.length > 2) {
                      if (goodsCode == "" ||
                          goodsCode.substring(0, 2) == "QR") {
                        Common.showTost("商品条码格式错误");
                        return;
                      } else {
                        changeGoods(goodsCode);
                      }
                    } else {
                      Common.showTost("商品条码格式错误");
                    }
                  },
                ),
              ),
            ],

            // mainAxisAlignment: MainAxisAlignment.start,
          ),
        ));
  }
}
